# coding:utf-8
from models import meizitu_thread
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import base64

import os
import re
from models import *

from settings import *
import  urllib3
import re
import time

engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)

session = Session()

from selenium import webdriver
import selenium


class JiandanCrawer(object):
    base_url = 'http://jandan.net/ooxx/'

    def __init__(self):
        self.driver = webdriver.Firefox()
        self.driver.get(self.get_url())
        _page_number = self.driver.find_element_by_css_selector(".current-comment-page").text
        self.latest = int(re.search("(\d+)", _page_number).groups()[0])

        self.init()

    def get_url(self, _id=0):
        if _id == 0:
            return self.base_url
        else:
            return self.base_url + "page-{0}#comments".format(_id)

    def init(self):
        for i in range(self.latest, 0, -1):
            self.update(i)

    def start(self):
        while True:
            self.update(0)
            time.sleep(300)

    def add_thread(self, author, url, oo, xx):
        thread = session.query(jiandan_meizitu).filter_by(url=url).first()
        if not thread:
            thread = jiandan_meizitu(author=author, url=url, oo=oo, xx=xx, status="new")
            session.add(thread)
            session.commit()
            print("添加条目 "+ url)
        else:
            thread.oo = oo
            thread.xx = xx
            session.commit()
            print("更新条目" + url)
        return thread


    def add_tucao(self, thread, tucao_author, tucao_content):
        tucao = session.query(jiandan_tucao).filter_by(jiandan_meizitu_id=thread.id,
                    name=tucao_author, comment=tucao_content).first()
        if not tucao:
            session.add(jiandan_tucao(jiandan_meizitu_id=thread.id, name=tucao_author, comment=tucao_content))
            session.commit()

    def update(self, _id=1):
        print("开始抓取", self.get_url(_id))
        self.driver.get(self.get_url(_id))
        items = self.driver.find_elements_by_css_selector("ol.commentlist li")
        for item in items:
            if item.get_attribute("id").startswith("comment"):
                while True:
                    try:
                        print(item.find_element_by_css_selector("div.author").text)
                        author = item.find_element_by_css_selector("div.author").text.split("@")[0].strip()
                        print(item.find_element_by_css_selector("p img").get_attribute("src"))
                        url = item.find_element_by_css_selector("p img").get_attribute("src")
                        print(item.find_element_by_css_selector("span.tucao-like-container span").text)
                        oo = item.find_element_by_css_selector("span.tucao-like-container span").text
                        print(item.find_element_by_css_selector("span.tucao-unlike-container span").text)
                        xx = item.find_element_by_css_selector("span.tucao-unlike-container span").text
                        print(int(re.search("(\d+)", item.find_element_by_css_selector(".tucao-btn").text).groups()[0]))
                        tucao_num = int(re.search("(\d+)", item.find_element_by_css_selector(".tucao-btn").text).groups()[0])
                        break
                    except selenium.common.exceptions.StaleElementReferenceException:
                        time.sleep(2)
                        print("loading ....")
                thread = self.add_thread(author, url, oo, xx)
                if tucao_num != 0:
                    item.find_element_by_css_selector(".tucao-btn").click()
                    while True:
                        try:
                            tucaos = item.find_elements_by_css_selector(".tucao-list div.tucao-row")
                            break
                        except selenium.common.exceptions.StaleElementReferenceException:
                            time.sleep(2)
                            print("loading ....")
                    for tucao in tucaos:
                        tucao_author = tucao.find_element_by_css_selector(".tucao-author span").text
                        tucao_content = tucao.find_element_by_css_selector(".tucao-content").text
                        self.add_tucao(thread, tucao_author, tucao_content)


jiandanCrawer = JiandanCrawer()
#jiandanCrawer.start()
